package com.cosina.game.crystallight.engine;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import android.graphics.Point;

import com.cosina.game.crystallight.MediaPlayerAdapter;
import com.cosina.game.crystallight.engine.util.Time;
import com.cosina.game.crystallight.model.battle.Ability;
import com.cosina.game.crystallight.model.battle.BattleField;
import com.cosina.game.crystallight.model.crystal.Tower;

public class Architect implements Time{

	private SortedSet<Tower> towers = new TreeSet<Tower>();
	private List<Point> puddleLocations = new ArrayList<Point>();
	private List<Point> vortexLocations = new ArrayList<Point>();
	public Ability buildAbility = null; //no buliding model;
	public static final int red = 0;
	public static final int green = 1;
	public Architect() {
		for(Point point : BattleEngine.field.defaultTowers()){
			towers.add(new Tower(point));
		}
	}

	
	public void buildTower(int x ,int y){
		BattleEngine.statistics.towerBuilt++;
		towers.add(new Tower(x,y));
	}
	
	public void build(int x, int y){
		if(isBuildModel()){
			int[][] mapInfo = getBuildingMap();
			if(mapInfo[y][x] == green){
				switch (buildAbility.getId()) {
					case Ability.TOWER:
						MediaPlayerAdapter.buildTower();
						buildTower(x,y);
						break;
					case Ability.PUDDLE:
						MediaPlayerAdapter.buildPuddle();
						puddleLocations.add(new Point(x,y));
						break;
					case Ability.VORTEX:
						MediaPlayerAdapter.buildVortex();
						vortexLocations.add(new Point(x,y));
						break;
					default:
						throw new IllegalArgumentException("what's going on?");
				}
				BattleEngine.manaFlask.used(buildAbility.needMana());
				buildAbility = null;
			}
		}
	}
	
	
	public Collection<Tower> getTowers(){
		return towers;
	}
	
	public List<Point> getVortexLocations(){
		return vortexLocations;
	}
	
	public List<Point> getPuddleLocations(){
		return puddleLocations;
	}
	
					//
	public boolean isBuildModel(){
		return buildAbility != null;
	}
	
	public int[][] getBuildingMap(){
		if(buildAbility ==null)
			return new int[0][0];
		
		int[][] baseGround = BattleEngine.field.groudData();
		int height = baseGround.length;
		int width = baseGround[0].length;
		int[][] result = new int[height][width];
		
		if(buildAbility.getId() == Ability.TOWER){
			for(int y=0; y<height; y++){
				for(int x=0; x<width; x++) {
					if(baseGround[y][x] == BattleField.TILE_TYPE_EARTH){
						result[y][x] = green;
					}else{
						result[y][x] = red;
					}
				}
			}
			for(Tower eachTower : getTowers()){
				result[eachTower.location.y][eachTower.location.x] = red;
			}
			return result;
		}
		if(buildAbility.getId() == Ability.PUDDLE){
			for(int y=0; y<9; y++){
				for(int x=0; x<7; x++) {
					if(baseGround[y][x] < 0){
						result[y][x] = green;
					}else{
						result[y][x] = red;
					}
				}
			}
			for(Point point : BattleEngine.field.getCastleArea()){
				result[point.y][point.x] = red;
			}
			for(Point point : BattleEngine.field.getEnemyHomeArea()){
				result[point.y][point.x] = red;
			}
			for(Point point : this.getPuddleLocations()){
				result[point.y][point.x] = red;
			}
			return result;
		}if(buildAbility.getId() == Ability.VORTEX){
			for(int y=0; y<9; y++){
				for(int x=0; x<7; x++) {
					if(baseGround[y][x] < 0){
						result[y][x] = green;
					}else{
						result[y][x] = red;
					}
				}
			}
			for(Point point : BattleEngine.field.getCastleArea()){
				result[point.y][point.x] = red;
			}
			for(Point point : BattleEngine.field.getEnemyHomeArea()){
				result[point.y][point.x] = red;
			}
			for(Point point : this.getVortexLocations()){
				result[point.y][point.x] = red;
			}
			return result;
		}
		
		return new int[0][0];
	}

	@Override
	public void withTimeGo() {
		for(Tower tower : towers){
			tower.withTimeGo();
		}
	}
}
